package com.right.near.business.service.impl;

import com.right.near.business.entity.User;
import com.right.near.business.model.dto.UserDetailDTO;
import com.right.near.business.service.IUserService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.Arrays;

/**
 * s
 *
 * @author: Star.zhu
 * @date: 2022/8/12
 */

@Slf4j
@Service
@AllArgsConstructor
public class UserDetailsServiceImpl implements UserDetailsService {

    private final IUserService userService;

    /**
     * 用户名称登陆
     *
     * @param username 用户名称
     * @return
     * @throws UsernameNotFoundException
     */
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        Assert.hasText(username, "用户名称不能为空");
        User user = userService.queryUserByUserName(username);
        Assert.notNull(user, "用户不存在");
        UserDetailDTO userDetail = new UserDetailDTO();
        userDetail.setId(user.getId());
        userDetail.setUserName(user.getUserName());
        userDetail.setRoles(Arrays.asList("ADMIN"));
        userDetail.setPassword(user.getPassword());
        userDetail.setEnabled(0 == user.getIsEnabled());
        userDetail.setLocked(true);
        return userDetail;
    }

}
